/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.fonzie.acceptance;

import static junit.framework.Assert.assertEquals;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import org.junit.Before;
import org.junit.Test;

import fr.fonzie.Fonzie;
import fr.fonzie.acceptance.model.User;

public class SimpleEntityManagerLifeCycleTest
{
    private EntityManager em;

    private User user;

    @Before
    public void setup()
    {
        user = new User();
        EntityManagerFactory emf = createMock( EntityManagerFactory.class );
        em = createMock( EntityManager.class );
        expect( emf.createEntityManager() ).andReturn( em ).anyTimes();
        replay( emf );
        Fonzie.configure( emf, new StubQueryParser() );
    }

    @Test
    public void persist()
    {
        em.persist( user );
        replay( em );
        user.persist();
        verify( em );
    }

    @Test
    public void remove()
    {
        em.remove( user );
        replay( em );
        user.remove();
        verify( em );
    }

    @Test
    public void refresh()
    {
        em.refresh( user );
        replay( em );
        user.refresh();
        verify( em );
    }

    @Test
    public void detach()
    {
        em.detach( user );
        replay( em );
        user.detach();
        verify( em );
    }

    @Test
    public void isManaged()
    {
        expect( em.contains( user ) ).andReturn( true );
        replay( em );
        user.isManaged();
        verify( em );
    }

    @Test
    public void find()
    {
        expect( em.find( User.class, 1L ) ).andReturn( user );
        replay( em );
        assertEquals( user, User.find( 1L ) );
        verify( em );
    }

    @Test
    public void getReference()
    {
        expect( em.getReference( User.class, 1L ) ).andReturn( user );
        replay( em );
        assertEquals( user, User.getReference( 1L ) );
        verify( em );
    }

    @Test
    public void findByName()
    {
        @SuppressWarnings( "unchecked" )
        TypedQuery<User> q = createMock( TypedQuery.class );
        expect( em.createQuery( "Name", User.class ) ).andReturn( q );
        expect( q.getSingleResult() ).andReturn( user );
        replay( em, q );
        assertEquals( user, User.findByName( "test" ) );
        verify( em );
    }
}
